package jmind.core.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;

public class TestImage {
	/**
	* 加密
	* @param s
	* @return
	*/
	static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String md5(byte[] s) {
		//16进制字符

		try {
			byte[] strTemp = s;
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			//移位 输出字符串
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 读取字节数
	 * @param name
	 * @return
	 */
	public static byte[] getByte(String name) {
		//得到文件长度
		File file = new File(name);
		byte[] b = new byte[(int) file.length()];
		System.out.println(file.length());
		try {
			InputStream in = new FileInputStream(file);
			try {
				in.read(b);
				System.out.println(in.read());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return b;
	}

	public static void main(String[] args) throws Exception {

		String img1 = md5(getByte("c:/c.jpg"));
		String img2 = md5(getByte("c:/d.jpg"));
		if (img1.equals(img2))
			System.out.println("两图片是一样的");
		else
			System.out.println("两图片是不一样的");
	}

}
